package com.boot.demo.config.web;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration //说明是一个配置文件
@EnableWebMvc  //开启springMVC 模式
public class MvcConfiguration implements WebMvcConfigurer , ApplicationContextAware {
	//spring容器
	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;	
	}
	
	//1.配置静态资源
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		
	}
	
	//2.配置默认请求
	@Override
	public void  configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
	//3.配置视图解析器
	@Bean("viewResolver")
	public InternalResourceViewResolver createViewResolver() {
		//创建对象
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		//注入spring容器
		viewResolver.setApplicationContext(this.applicationContext);
		//设置路径
		viewResolver.setPrefix("/WEB-INF/html/");
		//设置扩展名
		viewResolver.setSuffix(".html");
		
		return viewResolver;
	}
	
}
































//@Configuration
//@EnableWebMvc  //开启springMVC注解模式,== <mvc:annotation-driven/>
//public class MvcConfiguration implements WebMvcConfigurer, ApplicationContextAware{
//	//spring容器
//	private ApplicationContext applicationContext;
//	@Override
//	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//		this.applicationContext = applicationContext;
//	}
//	
//	//静态资源配置
//	@Override
//	public void addResourceHandlers(ResourceHandlerRegistry registry) {
//		//registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
//	}
//	
//	//默认请求处理器Servlet
//	@Override
//	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//		configurer.enable();
//	}
//	
//	//视图解析器
//	@Bean(name="viewResolver")
//	public ViewResolver createViewResolver() {
//		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
//		//配置spring容器
//		viewResolver.setApplicationContext(this.applicationContext);
//		//关闭缓存
//		viewResolver.setCache(false);
//		//配置页面映射路径
//		viewResolver.setPrefix("/WEB-INF/html/");
//		//配置页面扩展名
//		viewResolver.setSuffix(".html");
//		
//		return viewResolver;
//	}
//}
